package com.gis507.mapreduce.analysis_index;

import com.gis507.mapreduce.AISBean.AnalysisIndex;
import com.gis507.mapreduce.Utils.UtilsClass;
import lombok.SneakyThrows;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Partitioner;
import org.junit.Test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author leon
 * @ClassName AnalysisPartitioner.java
 * @createTime 2021年09月17日 10:43:00
 */
public class AnalysisPartitioner extends Partitioner<Text, AnalysisIndex> {


    SimpleDateFormat fo = new SimpleDateFormat("yyyy-MM-dd:HH:mm:SS");
    String time1 = "2020-08-05:12:00:00";
    String time2 = "2020-08-10:12:00:00";
    String time3 = "2020-08-15:12:00:00";
    String time4 = "2020-08-20:12:00:00";
    String time5 = "2020-08-25:12:00:00";
    String time6 = "2020-08-30:12:00:00";



    @SneakyThrows
    @Override
    public int getPartition(Text text, AnalysisIndex ais, int i){

        Long data1 = UtilsClass.getTime(time1, fo);
        Long data2 = UtilsClass.getTime(time2, fo);
        Long data3 = UtilsClass.getTime(time3, fo);
        Long data4 = UtilsClass.getTime(time4, fo);
        Long data5 = UtilsClass.getTime(time5, fo);
        Long data6 = UtilsClass.getTime(time6, fo);
        if (ais.getUnixTime() * 1000L < data1)
            return 0;
        else if (ais.getUnixTime() * 1000L < data2)
            return 1;
        else if (ais.getUnixTime() * 1000L < data3)
            return 2;
        else if (ais.getUnixTime() * 1000L < data4)
            return 3;
        else if (ais.getUnixTime() * 1000L < data5)
            return 4;
        else
            return 5;
    }

    @Test
    public void test() throws ParseException {
//        long l = System.currentTimeMillis();
        SimpleDateFormat fo = new SimpleDateFormat("yyyy-MM-dd:HH:mm:SS");
        String format = fo.format(new Date(1596354456000L));
//        String time = "2021-09-16:09:00:00";
//        long time1 = fo.parse(time).getTime();
        System.out.println(format);
//        System.out.println(time1);
//        Long time6 = UtilsClass.getTime(time1, fo);
//        System.out.println(time6);
    }




}
